package com.example.parttimejob.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.parttimejob.mapper.PartTimeJobsMapper;
import com.example.parttimejob.mapper.StoreMapper;
import com.example.parttimejob.mapper.UserMapper;
import com.example.parttimejob.pojo.entity.Merchants;
import com.example.parttimejob.pojo.entity.PartTimeJobs;
import com.example.parttimejob.pojo.entity.User;
import com.example.parttimejob.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceimpl implements AdminService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private PartTimeJobsMapper partTimeJobsMapper;

    @Override
    public void CheckUserInfo(Integer id ,String status) {
        User user=userMapper.selectById(id);
        if(status.equals("已实名")) {
            user.setAccountStatus(status);
            userMapper.updateById(user);
        }
        else
        {
            User user1=new User();
            user1.setUserId(user.getUserId());
            user1.setUsername(user.getUsername());
            user1.setPasswd(user.getPasswd());
            user1.setAccountStatus(status);
            userMapper.deleteById(user);
            userMapper.insert(user1);
        }
        }

    @Override
    public void CheckStoreInfo(Integer id,String status) {
        Merchants merchants=storeMapper.selectById(id);
        if(status.equals("已实名")) {
            merchants.setAccountStatus(status);
            storeMapper.updateById(merchants);
        }
        else
        {
            Merchants merchants1=new Merchants();
            merchants1.setMerchantId(merchants.getMerchantId());
            merchants1.setMerchantUsername(merchants.getMerchantUsername());
            merchants1.setPasswd(merchants.getPasswd());
            merchants1.setAccountStatus(status);
            storeMapper.deleteById(merchants);
            storeMapper.insert(merchants1);
        }
    }



    @Override
    public void CheckJobInfo(Integer id, String status) {
        PartTimeJobs partTimeJobs=partTimeJobsMapper.selectById(id);
        if(status.equals("未请求")) {
            partTimeJobs.setJobStatus(status);
            partTimeJobsMapper.updateById(partTimeJobs);
        }
        else
        {
            partTimeJobsMapper.deleteById(partTimeJobs);
        }
    }

    @Override
    public List<User> GetCheckUserInfo() {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account_status","待管理员审核");
        return userMapper.selectList(queryWrapper);
    }

    @Override
    public List<Merchants> GetCheckStoreInfo() {
        QueryWrapper<Merchants> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account_status","待管理员审核");
        return storeMapper.selectList(queryWrapper);
    }

    @Override
    public List<PartTimeJobs> GetCheckJobInfo() {
        QueryWrapper<PartTimeJobs> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("job_status","待管理员审核");
        return partTimeJobsMapper.selectList(queryWrapper);
    }

    @Override
    public List<User> User() {
        return userMapper.selectList(null);
    }

    //用户模糊查询
    @Override
    public List<User> SearchUser(String username) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        if(username!=null) {
            queryWrapper.like("username", username);
            return userMapper.selectList(queryWrapper);
        }
        else
        {
            return userMapper.selectList(null);
        }
        }


    @Override
    public String BanUser(Integer id) {
    User user=userMapper.selectById(id);
    if(user.getVerificationStatus()==0)
    {
    user.setVerificationStatus(1);
    userMapper.updateById(user);
    return "封禁成功!";
    }
    else
    {
        return "账号已封禁!";
    }
    }

    @Override
    public List<Merchants> Store() {
        return storeMapper.selectList(null);
    }

    @Override
    public String BanStore(Integer id) {
        Merchants merchants=storeMapper.selectById(id);
        if(merchants.getVerificationStatus()==0)
        {
            merchants.setVerificationStatus(1);
            storeMapper.updateById(merchants);
            return "封禁成功!";
        }
        else
        {
            return "账号已封禁!";
        }
    }

    @Override
    public List<Merchants> SearchStore(String username) {
        QueryWrapper<Merchants> queryWrapper=new QueryWrapper<>();
        if(username!=null) {
            queryWrapper.like("merchant_name", username);
            return storeMapper.selectList(queryWrapper);
        }
        else
        {
            return storeMapper.selectList(null);
        }
    }

    @Override
    public List<User> SearchCheckUser(String username) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        if(username!=null) {
            queryWrapper.like("username", username).eq("account_status", "待管理员审核");
            return userMapper.selectList(queryWrapper);
        }
        else
        {
            queryWrapper.eq("account_status", "待管理员审核");
            return userMapper.selectList(queryWrapper);
        }
    }

    @Override
    public List<Merchants> SearchCheckStore(String username) {
        QueryWrapper<Merchants> queryWrapper=new QueryWrapper<>();
        if(username!=null) {
            queryWrapper.like("merchant_name", username).eq("account_status", "待管理员审核");
            return storeMapper.selectList(queryWrapper);
        }
        else
        {
            queryWrapper.eq("account_status", "待管理员审核");
            return storeMapper.selectList(queryWrapper);
        }
    }

    @Override
    public List<PartTimeJobs> SearchCheckJob(String username) {
        QueryWrapper<PartTimeJobs> queryWrapper=new QueryWrapper<>();
        if(username!=null) {
            queryWrapper.like("title", username).eq("job_status", "待管理员审核");
            return partTimeJobsMapper.selectList(queryWrapper);
        }
        else
        {
            queryWrapper.eq("job_status", "待管理员审核");
            return  partTimeJobsMapper.selectList(queryWrapper);
        }
    }
}
